home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / snmp-dev.000 / snmp-dev / examples / snmpget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  9.9 KB  |  335 lines

  1. /*
  2.  * snmpget.c - send snmp GET requests to a network entity.
  3.  *
  4.  */
  5. /***********************************************************************
  6.     Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <sys/time.h>
  31. #ifdef linux
  32. # include <stdlib.h>
  33. # include <string.h>
  34. # include <netdb.h>
  35. # include <arpa/inet.h>
  36. #endif
  37.  
  38. #include <snmp/snmp.h>
  39. #include <snmp/asn1.h>
  40. #include <snmp/mib.h>
  41. #include <snmp/snmp_impl.h>
  42. #include <snmp/snmp_api.h>
  43. #include <snmp/snmp_client.h>
  44. #include <snmp/party.h>
  45. #include <snmp/context.h>
  46. #include <snmp/view.h>
  47. #include <snmp/acl.h>
  48.  
  49. extern int  errno;
  50. int    snmp_dump_packet = 0;
  51.  
  52. void
  53. usage(){
  54.     fprintf(stderr, "Usage: snmpget -v 1 hostname community objectID [objectID]*      or:\n");
  55.     fprintf(stderr, "Usage: snmpget [-v 2 ] hostname noAuth objectID [objectID]*      or:\n");
  56.     fprintf(stderr, "Usage: snmpget [-v 2 ] hostname srcParty dstParty context objectID [objectID]*\n");
  57. }
  58.  
  59. int
  60. main(argc, argv)
  61.     int        argc;
  62.     char    *argv[];
  63. {
  64.     struct snmp_session session, *ss;
  65.     struct snmp_pdu *pdu, *response;
  66.     struct variable_list *vars;
  67.     int    arg;
  68.     char *hostname = NULL;
  69.     char *community = NULL;
  70.     int timeout_flag = 0, timeout = 0, retransmission_flag = 0;
  71.     int retransmission = 0;                /* YYY: check init */
  72.     int    count, current_name = 0;
  73.     char *names[128];
  74.     oid name[MAX_NAME_LEN];
  75.     int name_length;
  76.     int status;
  77.     int version = 2;
  78.     int port_flag = 0;
  79.     int dest_port = 0;
  80.     u_long srcclock = 0, dstclock = 0;            /* YYY: check init */
  81.     int clock_flag = 0;
  82.     oid src[MAX_NAME_LEN], dst[MAX_NAME_LEN], context[MAX_NAME_LEN];
  83.     int srclen = 0, dstlen = 0, contextlen = 0;
  84.     struct partyEntry *pp;
  85.     struct contextEntry *cxp;
  86.     int trivialSNMPv2 = FALSE;
  87.     struct hostent *hp;
  88.     u_long destAddr;
  89.  
  90.  
  91.  
  92.     init_mib();
  93.     for(arg = 1; arg < argc; arg++){
  94.     if (argv[arg][0] == '-'){
  95.         switch(argv[arg][1]){
  96.         case 'd':
  97.             snmp_dump_packet++;
  98.             break;
  99.                 case 'p':
  100.                     port_flag++;
  101.                     dest_port = atoi(argv[++arg]);
  102.                     break;
  103.                 case 't':
  104.                     timeout_flag++;
  105.                     timeout = atoi(argv[++arg]) * 1000000L;
  106.                     break;
  107.                 case 'r':
  108.                     retransmission_flag++;
  109.                     retransmission = atoi(argv[++arg]);
  110.                     break;
  111.                 case 'c':
  112.                     clock_flag++;
  113.                     srcclock = atoi(argv[++arg]);
  114.                     dstclock = atoi(argv[++arg]);
  115.                     break;
  116.                 case 'v':
  117.                     version = atoi(argv[++arg]);
  118.                     if (version < 1 || version > 2){
  119.                         fprintf(stderr, "Invalid version\n");
  120.                         usage();
  121.                         exit(1);
  122.                     }
  123.                     break;
  124.         default:
  125.             printf("invalid option: -%c\n", argv[arg][1]);
  126.             break;
  127.         }
  128.         continue;
  129.     }
  130.     if (hostname == NULL){
  131.         hostname = argv[arg];
  132.         } else if (version == 1 && community == NULL){
  133.             community = argv[arg];
  134.     } else if (version == 2 && srclen == 0 && !trivialSNMPv2){
  135.         if (read_party_database(party_conf ()) > 0){
  136.         fprintf(stderr,
  137.             "Couldn't read party database from %s\n",
  138.                 party_conf ());
  139.         exit(0);
  140.         }
  141.             if (read_context_database(context_conf ()) > 0){
  142.                 fprintf(stderr,
  143.                         "Couldn't read context database from %s\n",
  144.             context_conf ());
  145.                exit(0);
  146.             }
  147.         if (read_acl_database(acl_conf ()) > 0){
  148.         fprintf(stderr,
  149.             "Couldn't read access control database from %s\n",
  150.                 acl_conf ());
  151.         exit(0);
  152.         }
  153.             if (!strcasecmp(argv[arg], "noauth")){
  154.                 trivialSNMPv2 = TRUE;
  155.             } else {
  156.                 party_scanInit();
  157.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  158.             if (!strcasecmp(pp->partyName, argv[arg])){
  159.             srclen = pp->partyIdentityLen;
  160.             bcopy(pp->partyIdentity, src, srclen * sizeof(oid));
  161.             break;
  162.             }
  163.         }
  164.         if (!pp){
  165.             srclen = MAX_NAME_LEN;
  166.             if (!read_objid(argv[arg], src, &srclen)){
  167.             printf("Invalid source party: %s\n", argv[arg]);
  168.             srclen = 0;
  169.             usage();
  170.             exit(1);
  171.             }
  172.         }
  173.         }
  174.     } else if (version == 2 && dstlen == 0 && !trivialSNMPv2){
  175.         dstlen = MAX_NAME_LEN;
  176.         party_scanInit();
  177.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  178.         if (!strcasecmp(pp->partyName, argv[arg])){
  179.             dstlen = pp->partyIdentityLen;
  180.             bcopy(pp->partyIdentity, dst, dstlen * sizeof(oid));
  181.             break;
  182.         }
  183.         }
  184.         if (!pp){
  185.         if (!read_objid(argv[arg], dst, &dstlen)){
  186.             printf("Invalid destination party: %s\n", argv[arg]);
  187.             dstlen = 0;
  188.             usage();
  189.             exit(1);
  190.         }
  191.         }
  192.         } else if (version == 2 && contextlen == 0 && !trivialSNMPv2){
  193.             contextlen = MAX_NAME_LEN;
  194.             context_scanInit();
  195.             for(cxp = context_scanNext(); cxp; cxp = context_scanNext()){
  196.                 if (!strcasecmp(cxp->contextName, argv[arg])){
  197.                     contextlen = cxp->contextIdentityLen;
  198.                     bcopy(cxp->contextIdentity, context,
  199.                           contextlen * sizeof(oid));
  200.                     break;
  201.                 }
  202.             }
  203.             if (!cxp){
  204.                 if (!read_objid(argv[arg], context, &contextlen)){
  205.                     printf("Invalid context: %s\n", argv[arg]);
  206.                     contextlen = 0;
  207.             usage();
  208.                     exit(1);
  209.                 }
  210.             }
  211.     } else {
  212.         names[current_name++] = argv[arg];
  213.     }
  214.     }
  215.  
  216.     if (!hostname || current_name <= 0 || (version < 1) || (version > 2)
  217.     || (version == 1 && !community)
  218.     || (version == 2 && (!srclen || !dstlen || !contextlen)
  219.         && !trivialSNMPv2)){
  220.     usage();
  221.     exit(1);
  222.     }
  223.  
  224.     if (trivialSNMPv2){
  225.     if ((destAddr = inet_addr(hostname)) == -1){
  226.         hp = gethostbyname(hostname);
  227.         if (hp == NULL){
  228.         fprintf(stderr, "unknown host: %s\n", hostname);
  229.         exit(1);
  230.         } else {
  231.         bcopy((char *)hp->h_addr, (char *)&destAddr,
  232.               hp->h_length);
  233.         }
  234.     }
  235.     srclen = dstlen = contextlen = MAX_NAME_LEN;
  236.     ms_party_init(destAddr, src, &srclen, dst, &dstlen,
  237.               context, &contextlen);
  238.     }
  239.  
  240.     if (clock_flag){
  241.         pp = party_getEntry(src, srclen);
  242.         if (pp){
  243.             pp->partyAuthClock = srcclock;
  244.             gettimeofday(&pp->tv, (struct timezone *)0);
  245.             pp->tv.tv_sec -= pp->partyAuthClock;
  246.         }
  247.         pp = party_getEntry(dst, dstlen);
  248.         if (pp){
  249.             pp->partyAuthClock = dstclock;
  250.             gettimeofday(&pp->tv, (struct timezone *)0);
  251.             pp->tv.tv_sec -= pp->partyAuthClock;
  252.         }
  253.     }
  254.  
  255.     bzero((char *)&session, sizeof(struct snmp_session));
  256.     session.peername = hostname;
  257.     if (port_flag)
  258.         session.remote_port = dest_port;
  259.  
  260.     if (version == 1){
  261.         session.version = SNMP_VERSION_1;
  262.         session.community = (u_char *)community;
  263.         session.community_len = strlen((char *)community);
  264.     } else if (version == 2){
  265.         session.version = SNMP_VERSION_2;
  266.         session.srcParty = src;
  267.         session.srcPartyLen = srclen;
  268.         session.dstParty = dst;
  269.         session.dstPartyLen = dstlen;
  270.         session.context = context;
  271.         session.contextLen = contextlen;
  272.     }
  273.     if (retransmission_flag)
  274.         session.retries = retransmission;
  275.     else
  276.         session.retries = SNMP_DEFAULT_RETRIES;
  277.     if (timeout_flag)
  278.         session.timeout = timeout;
  279.     else
  280.         session.timeout = SNMP_DEFAULT_TIMEOUT;
  281.  
  282.     session.authenticator = NULL;
  283.     snmp_synch_setup(&session);
  284.     ss = snmp_open(&session);
  285.     if (ss == NULL){
  286.     printf("Couldn't open snmp\n");
  287.     exit(-1);
  288.     }
  289.  
  290.     pdu = snmp_pdu_create(GET_REQ_MSG);
  291.  
  292.     for(count = 0; count < current_name; count++){
  293.     name_length = MAX_NAME_LEN;
  294.     if (!read_objid(names[count], name, &name_length)){
  295.         printf("Invalid object identifier: %s\n", names[count]);
  296.     }
  297.     
  298.     snmp_add_null_var(pdu, name, name_length);
  299.     }
  300.  
  301. retry:
  302.     status = snmp_synch_response(ss, pdu, &response);
  303.     if (status == STAT_SUCCESS){
  304.     if (response->errstat == SNMP_ERR_NOERROR){
  305.         for(vars = response->variables; vars; vars = vars->next_variable)
  306.         print_variable(vars->name, vars->name_length, vars);
  307.     } else {
  308.         printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
  309.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  310.         printf("This name doesn't exist: ");
  311.         for(count = 1, vars = response->variables; vars && count != response->errindex;
  312.             vars = vars->next_variable, count++)
  313.             ;
  314.         if (vars)
  315.             print_objid(vars->name, vars->name_length);
  316.         printf("\n");
  317.         }
  318.         if ((pdu = snmp_fix_pdu(response, GET_REQ_MSG)) != NULL)
  319.         goto retry;
  320.     }
  321.  
  322.     } else if (status == STAT_TIMEOUT){
  323.     printf("No Response from %s\n", hostname);
  324.     } else {    /* status == STAT_ERROR */
  325.     printf("An error occurred, Quitting\n");
  326.     }
  327.  
  328.     if (response)
  329.     snmp_free_pdu(response);
  330.     snmp_close(ss);
  331.  
  332.     return 0;
  333. }
  334.  
  335.